﻿@using System.Collections
@using Coralcode.Adapter.TextualAnalyzer.Entity.Domains
@using Coralcode.Base.Entity
@using Coralcode.Base.Utils
@using CoralNode.Core.Entity.Domains.User
@using CoralNode.Core.Entity.Enums
@using CoralNode.MvcClient.Helper
@using CoralNode.Core.Entity.Domains.Prototype
@using CoralNode.MvcClient.Contexts
@model IList<SearchIndexDomain>
@{
    var searchIndexs = Model ?? new List<SearchIndexDomain>();
    IList<PrototypeDomain> prototypes = ViewBag.Prototypes;
}
@Html.IncludePage("CoralAdmin/sitesetting", "_Left", new { current = "search" })
<style>
    .checkbox_own { margin-top: 6px; }
</style>
@{
    long[] prototypeIds = ViewBag.PrototypeIds;
}
<div id="pbody">
    <div class="extra">
        <div class="pbody_top">
            <span class="ico ico_setting_system"></span><span class="t t_c">网站设置&nbsp;&nbsp;/&nbsp;&nbsp;<b
                class=" t_small f_no">搜索设置</b></span>
        </div>
        <div id="main" class="userList_cnt">
            <div class="content_tab extra_normal">
                <div class="tablist extra_normal">
                    <a href="/m/sitesetting/sitesearchsetting">搜索设置</a>
                    <a href="/m/sitesetting/siteindexsetting" class="current">站点索引</a>
                </div>
                <a class="btn_5 f_r mr_1 mt_0-5 js_searchIndex" href="/m/sitesetting/SaveSiteIndexSetting">
                    添加索引</a>
            </div>
            <div class="pt_1 js_tabcnt ">
                <div class="tablelist_style1">
                    <div class="p_1">
                        @if (searchIndexs.Count > 0) {
                            <div class="js_tab">
                                @foreach (var item in searchIndexs) {
                                    <a href="javascript:">@item.Title<span class="del" data-href="/m/sitesetting/deleteindex/@item.Id"></span></a>
                                }
                            </div>
                            <div class="pt_1">
                                @foreach (var index in searchIndexs) {
                                    var i = 4;
                                    <form action="/m/sitesetting/SaveSiteIndexSetting2" method="post" class="js_ajaxform">
                                        <div>
                                            <table cellpadding="0" cellspacing="0">
                                                <tr class="exclude">
                                                    <th width="100" align="center">ID</th>
                                                    <th width="300" align="left">字段名</th>
                                                    <th width="100" align="center">字段类型</th>
                                                    <th>索引设置</th>
                                                </tr>
                                                <tr>
                                                    <td>1</td>
                                                    <td class="td_left">标题</td>
                                                    <td width="td_left">系统内置</td>
                                                    <td>
                                                        @Html.Hidden("Fields[0].FieldName", "Title")
                                                        <select name="Fields[0].FieldType">
                                                            @foreach (var keyvalue in ViewBag.IndexActionList) {
                                                                var setting = index.Fields.FirstOrDefault(n => n.FieldName == "Title");
                                                                <option value="@keyvalue.Value" @if (setting != null && setting.FieldType == keyvalue.Value) {
                                                                                                    @String.Format("selected=selected")
                                                                                                } >@keyvalue.Key</option>
                                                            }
                                                        </select>
                                                    </td>
                                                </tr>
                                                <tr>
                                                    <td>2</td>
                                                    <td class="td_left">分类Id</td>
                                                    <td width="td_left">系统内置</td>
                                                    <td>
                                                        @Html.Hidden("Fields[1].FieldName", "CategoryId")
                                                        <select name="Fields[1].FieldType">
                                                            @foreach (var keyvalue in ViewBag.IndexActionList) {
                                                                var setting = index.Fields.FirstOrDefault(n => n.FieldName == "CategoryId");
                                                                <option value="@keyvalue.Value" @if (setting != null && setting.FieldType == keyvalue.Value) {
                                                                                            @String.Format("selected=selected")
                                                                                                } >@keyvalue.Key</option>
                                                            }
                                                        </select></td>
                                                </tr>
                                                <tr>
                                                    <td>3</td>
                                                    <td class="td_left">原型Id</td>
                                                    <td width="td_left">系统内置</td>
                                                    <td>
                                                        @Html.Hidden("Fields[2].FieldName", "PrototypeId")
                                                        <select name="Fields[2].FieldType">
                                                            @foreach (var keyvalue in ViewBag.IndexActionList) {
                                                                var setting = index.Fields.FirstOrDefault(n => n.FieldName == "PrototypeId");
                                                                <option value="@keyvalue.Value" @if (setting != null && setting.FieldType == keyvalue.Value) {
                                                                                            @String.Format("selected=selected")
                                                                                                } >@keyvalue.Key</option>
                                                            }
                                                        </select></td>
                                                </tr>
                                                <tr>
                                                    <td>4</td>
                                                    <td class="td_left">发布时间</td>
                                                    <td width="td_left">系统内置</td>
                                                    <td>
                                                        @Html.Hidden("Fields[3].FieldName", "CreateTime")
                                                        <select name="Fields[3].FieldType">
                                                            @foreach (var keyvalue in ViewBag.IndexActionList) {
                                                                var setting = index.Fields.FirstOrDefault(n => n.FieldName == "CreateTime");
                                                                <option value="@keyvalue.Value" @if (setting != null && setting.FieldType == keyvalue.Value) {
                                                                                            @String.Format("selected=selected")
                                                                                                } >@keyvalue.Key</option>
                                                            }
                                                        </select></td>
                                                </tr>
                                                @foreach (var item in index.Fields.Where(n => n.FieldName.Contains('.'))) {
                                                    var prototypeCode = item.FieldName.Substring(0, item.FieldName.IndexOf('.'));
                                                    var fieldCode = item.FieldName.Substring(item.FieldName.IndexOf('.') + 1);
                                                    var field = prototypes.FirstOrDefault(n => n.Code == prototypeCode).FieldList.FirstOrDefault(n => n.FieldName == fieldCode);
                                                    <tr>
                                                        <td>@item.Id</td>
                                                        <td class="td_left">@field.Title
                                                        </td>
                                                        @{
                                                    var filedType = EnumUtil.GetEnumDescription(NodeEnums.BasicFieldType, field.FieldType);
                                                    if (string.IsNullOrEmpty(filedType)) {
                                                        filedType = EnumUtil.GetEnumDescription(NodeEnums.FunctionFieldType, field.FieldType);
                                                    }
                                                        }
                                                        <td width="td_left">@filedType</td>
                                                        <td>
                                                            @Html.Hidden("Fields[" + i + "].FieldName", item.FieldName)
                                                            <select name="Fields[@i].FieldType">
                                                                @foreach (var keyvalue in ViewBag.IndexActionList) {

                                                                    <option value="@keyvalue.Value" @if (item.FieldType == keyvalue.Value) {
                                                                                                        @String.Format("selected=selected")
                                                                                                    } >@keyvalue.Key</option>
                                                                }
                                                            </select>
                                                        </td>
                                                    </tr>
                                                                i++;
                                                }
                                            </table>
                                            <div class="submitbox p_2 extra_normal">
                                                @Html.Hidden("Id", index.Id)
                                                <label class="mr_2">
                                                    <input name="rebuildIndex" class="checkbox" type="checkbox" value=""><span>重建索引</span></label>
                                                <input type="submit" class="btn_1" value="提    交" />
                                                <a href="/m/sitesetting/SaveSiteIndexSetting/@index.Id" class="js_searchIndex ml_2">
                                                    编辑</a>
                                            </div>
                                        </div>
                                    </form>
                                }

                            </div>
                        }
                    </div>
                </div>
            </div>

        </div>
    </div>
</div>
<script>
    $(function () {

        $(".js_tab .del").click(function (e) {
            e.preventDefault();
            var $this = $(this);
            var href = $this.attr("data-href");
            $.confirm("您确定要删除这个索引吗？", function () {
                $.post(href, function (result) {
                    if (result.Type == 5) {
                        $.alert(result.Msg);
                        setTimeout("location.reload()", 1000);
                    } else {
                        $.alert(result.Msg, "error");
                    }
                });
            });
        });

        $(".js_ajaxform").submit(function (e) {
            e.preventDefault();
            var $form = $(this);
            var loaddingDialog = $.loading();
            if ($form.valid()) {
                $.post($form.attr("action"), $form.serialize(), function (data) {
                    loaddingDialog.close();
                    if (data.Type == 5) {
                        $.alert(data.Msg);

                    } else {
                        $.alert(data.Msg, "error");
                    }
                });
            }

            return false;
        });


        $(".js_updateindex").click(function () {
            $.post("/m/sitesetting/RebuildIndex", function (data) {
                if (data != null && data) {
                    $.alert("更新成功！");
                }
                else {
                    $.alert("更新失败！", "error");
                }
            });
        });
        $(".js_tab").each(function () {
            var $this = $(this);
            $this.tabs($this.next().children());
        });
        $(".js_searchIndex").click(function (e) {
            e.preventDefault();
            var $this = $(this);
            var dialog = $.dialog({
                title: "站点索引",
                content: '<img src="/Themes/CoralAdmin/_misc/images/loading.gif" style="display:block; margin:30px auto;" />',
                show: true,
                cancel: true,
                lock: true,
                ok: okfun,
                zIndex: 999
            });
            dialog.getJQuery().find(".d-state-highlight").css("margin-left", "140px");

            function okfun() {
                var $form = dialog.getJQuery().find("form");
                $.post($form.attr("action"), $form.serialize(), function (result) {
                    if (result.Type == 5) {
                        $.alert(result.Msg);
                        setTimeout(function () {
                            dialog.close();
                            location.reload();
                        }, 2000);
                    } else {
                        $.alert(result.Msg, "error");
                    }
                });
            }

            var $this = $(this);
            dialog.visible();
            $.get($this.attr("href"), function (data) {
                dialog.content(data);
            });
        });
    })
</script>
